/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.form.core;

import com.blandware.atleap.webapp.form.BaseForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Mab-backed form bean that handles set selections of checkboxes or simple properties
 * </p>
 * <p><a href="ProcessSetForm.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.6 $ $Date: 2006/08/03 10:08:06 $
 * @struts.form name="processSetForm"
 */
public class ProcessSetForm extends BaseForm {

	/**
	 * Selected elements
	 */
	protected List selectedItems = Collections.synchronizedList(new ArrayList());

	/**
	 * Holds all values for checked boxes. Need to have different field of type <code>java.util.Map</code>
	 * because there is no another way to get some value from checkbox. We can only know, was it checked or not.
	 */
	protected Map checkedBoxes = Collections.synchronizedMap(new HashMap());

	/**
	 * Creates the new instance of ProcessSetForm
	 */
	public ProcessSetForm() {
	}

	/**
	 * Returns element from specified position in list of selected items
	 *
	 * @param index Position of element to return
	 * @return Selected item with given position
	 */
	public Object getSelectedItem(int index) {
		if ( selectedItems != null && index > 0 && index < selectedItems.size() ) {
			return selectedItems.get(index);
		} else {
			return null;
		}
	}

	/**
	 * Replaces element on specified position in list, or adds it to the end of
     * list of selected items
	 *
	 * @param index Position to set element to. If index greater than or equal to list size, element will be added,
	 *              otherwise it will replace element, which is on specified position in list.
	 * @param value Value to set
	 */
	public void setSelectedItem(int index, Object value) {
		if ( log.isDebugEnabled() ) {
			log.debug("Setting element: index=" + index);
		}
		if ( index >= selectedItems.size() ) {
			selectedItems.add(value);
		} else {
			selectedItems.set(index, value);
		}
	}

    /**
     * Returns a list of selected elements
     *
     * @return list of selected elements
     */
	public List getSelectedItems() {
		return selectedItems;
	}

    /**
     * Sets a list of selected elements
     *
     * @param selectedItems list of selected elements to set
     */
	public void setSelectedItems(List selectedItems) {
		this.selectedItems = selectedItems;
	}

    /**
     * Returns a mapping from keys of checked boxes to some values (those values
     * don't matter; only their existance in map does matter)
     *
     * @return mapping from keys of checked boxes to some values
     */
	public Map getCheckedBoxes() {
		return checkedBoxes;
	}

    /**
     * Returns a mapping from keys of checked boxes to some values (those values
     * don't matter; only their existance in map does matter)
     *
     * @param checkedBoxes mapping from keys of checked boxes to some values
     */
	public void setCheckedBoxes(Map checkedBoxes) {
		this.checkedBoxes = checkedBoxes;
	}


	/**
	 * Returns <code>true</code>, if checkbox was checked
	 *
	 * @param key Key to look for. If it is exists in map, <code>Boolean.TRUE</code> will be returned
	 * @return <code>Boolean.TRUE</code> if key exists in map, and <code>Boolean.FALSE</code> otherwise
	 */
	public Object getCheckedBox(String key) {
		if ( checkedBoxes.containsKey(key) ) {
			return Boolean.TRUE;
		} else {
			return Boolean.FALSE;
		}
	}

	/**
	 * Puts key in map
	 *
	 * @param key   Key to associate value with
	 * @param value Value to put for specified key
	 */
	public void setCheckedBox(String key, Object value) {
		if ( log.isDebugEnabled() ) {
			log.debug("Putting pair: key=" + key + "; value= " + value);
		}
		checkedBoxes.put(key, value);
	}

	/**
	 * Resets all properties to their default values
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param request The non-http request we are proceeding
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.selectedItems = Collections.synchronizedList(new ArrayList());
		this.checkedBoxes = Collections.synchronizedMap(new HashMap());
	}

}